package cn.gwm.flink.streaming.strategy.termal;

import cn.gwm.flink.streaming.beans.termalRunaway.WaringTermalDwmWithStragegyPhev;
import cn.gwm.flink.streaming.beans.termalRunaway.WaringTermalWithStragegyAndFlagPhev;
import cn.gwm.flink.streaming.constant.TermalProcessResult;
import cn.gwm.flink.streaming.constant.TermalRunawayTypeEnum;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Objects;

/**
 * 车端热失控检测策略
 * @author GW00283474
 */
public class TermalVehicleCheckPhevStrategy {

    private static final Logger logger = LoggerFactory.getLogger(TermalVehicleCheckPhevStrategy.class);

    /**
     * 车端上传的热失控事件处理
     * @param dict
     * @return
     */
    public static Dict termalVehicleCheck(Dict dict){
        WaringTermalWithStragegyAndFlagPhev termalRecord = (WaringTermalWithStragegyAndFlagPhev)dict.get(TermalProcessResult.CHECK_RECORD);
        //车辆上传热失控事件 1：行车热失控  2：停车热失控
        Integer bms_battHeatRunaway = termalRecord.getBMS_BattHeatRunaway();
        if(Objects.nonNull(bms_battHeatRunaway)&&(bms_battHeatRunaway==1||bms_battHeatRunaway==2)){
            logger.info("进入到了车端上传的热失控事件处理,信号是：{}", JSONUtil.toJsonPrettyStr(termalRecord));
            String key = ""; String value = "";
            if(bms_battHeatRunaway==1){
                key = TermalRunawayTypeEnum.TERMALRUNAWAY.getKey();
                value= TermalRunawayTypeEnum.TERMALRUNAWAY.getValue();
            }else{
                key = TermalRunawayTypeEnum.TERMALRUNAWAYOFSTOP.getKey();
                value= TermalRunawayTypeEnum.TERMALRUNAWAYOFSTOP.getValue();
            }

            if(!termalRecord.checkFieldNullValue()){
                termalRecord.setTermalRunawayWarn(true);
                Dict checkDict = Dict.of(TermalProcessResult.MATCH, true,TermalProcessResult.TERMAL_EVENT_CODE,bms_battHeatRunaway,TermalProcessResult.RECENTLY_TID,termalRecord.getItem_time(),
                        TermalProcessResult.STRATEGY_KEY,key,TermalProcessResult.STRATEGY_DESC,value);
                ArrayList<WaringTermalDwmWithStragegyPhev> termalList=new ArrayList<>(0);
                WaringTermalDwmWithStragegyPhev ev= new WaringTermalDwmWithStragegyPhev();
                BeanUtil.copyProperties(termalRecord,ev);
                termalList.add(ev);
                checkDict.set(TermalProcessResult.CHECK_RECORDS,termalList);
                dict.set(TermalProcessResult.DICT_TERMAL_UPLOAD,checkDict);
                logger.info("车端上传的热失控事件，策略检测结果值为：{}",JSONUtil.toJsonPrettyStr(checkDict));
                return dict;
            }
        }else{
            Dict checkDict = Dict.of(TermalProcessResult.MATCH, false,
                    TermalProcessResult.TERMAL_EVENT_CODE,bms_battHeatRunaway,
                    TermalProcessResult.RECENTLY_TID, termalRecord.getItem_time(),
                    TermalProcessResult.STRATEGY_KEY, TermalRunawayTypeEnum.TERMALRUNAWAY.getKey() + "," + TermalRunawayTypeEnum.TERMALRUNAWAYOFSTOP.getKey(),
                    TermalProcessResult.STRATEGY_DESC, TermalRunawayTypeEnum.TERMALRUNAWAY.getValue() + "," + TermalRunawayTypeEnum.TERMALRUNAWAYOFSTOP.getValue());
            dict.set(TermalProcessResult.DICT_TERMAL_UPLOAD,checkDict);
            logger.info("车端上传的热失控事件校验通过，策略检测结果值为：{}",JSONUtil.toJsonPrettyStr(dict));
            return dict;
        }
        return null;
    }
}
